package com.baselibrary.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.util.Log;
import android.view.ViewTreeObserver;
import android.widget.TextView;


import com.baselibrary.R;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @author:hanke 2022/4/12
 * desc:
 */
public class SpanUtils {
    public static void setIcon(Context context, TextView text, String string) {
        //先设置原始文本
        text.setText(string);
        text.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                //这个监听的回调是异步的，在监听完以后一定要把绘制监听移除，不然这个会一直回调，导致界面错乱
                text.getViewTreeObserver().removeOnPreDrawListener(this);
                int line = text.getLineCount();
                if (line >= 2) {
                    text.setClickable(true);
                    //获取第一行的宽度
                    float lineWidth = text.getLayout().getLineWidth(0);
                    //获取第一行最后一个字符的下标
                    int lineEnd = text.getLayout().getLineEnd(0);
                    //计算每个字符占的宽度
                    float widthPerChar = lineWidth / (lineEnd + 1);
                    //计算TextView一行能够放下多少个字符
                    int numberPerLine = (int) Math.floor(text.getWidth() / widthPerChar);
                    StringBuilder stringBuilder;
                    if (text.getLayout().getEllipsisCount(text.getLineCount() - 1) > 0) {
                        String sub = string.substring(0, numberPerLine * text.getLineCount() - 4);
                        stringBuilder = new StringBuilder(sub).append("...").insert(numberPerLine - 1, "   ");
                    } else {
                        //在原始字符串中插入一个空格，插入的位置为numberPerLine - 1
                        stringBuilder = new StringBuilder(string).insert(numberPerLine - 1, " ");
                    }
                    SpannableString spannableString = new SpannableString(stringBuilder.toString() + "   ");
                    Drawable drawable = context.getResources().getDrawable(R.drawable.icon_black_arrow_right);
                    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                    ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
                    spannableString.setSpan(imageSpan, spannableString.length() - 1, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                    text.setText(spannableString);
                }else {
                    text.setMaxLines(2);
                    text.setClickable(false);
                }
                return true;
            }
        });
     }

    public static String appendEllipsis(String content) {
        if (content.length() > 6) {
            return new StringBuilder(content.substring(0, 6)).append("...").toString();
        }
        return content;
    }

    public static String appendCut(String content) {
        if (content != null) {
            if (content.length() > 12) {
                return new StringBuilder(content.substring(0, 12)).toString();
            } else {
                return content;
            }
        }
        return "";
    }

    public static String appendEllipsis(String content, int length) {
        if (content.length() > length) {
            return new StringBuilder(content.substring(0, length)).append("...").toString();
        }
        return content;
    }

    /**
     * 超出部分....
     * @param des
     * @param digit
     * @return
     */
    public static String substringContent(String des,int digit) {
        if (des.length() > digit) {
            des = des.substring(0, digit) + "......";
        }
        return des;
    }

    public static String subString(String content) {
        if (TextUtils.isEmpty(content)) return "";
        if (content.length() > 12) {
            return content.substring(0, 12);
        }
        return content;
    }

    /**
     * 搜索关键字标红
     * @return
     */
    public static void setSpecifiedTextsColor(Context context,TextView textView,String text, String specifiedTexts) {
        List<Integer> sTextsStartList = new ArrayList<>();

        int sTextLength = specifiedTexts.length();
        String temp = text;
        int lengthFront = 0;//记录被找出后前面的字段的长度
        int start = -1;
        do {
            start = temp.indexOf(specifiedTexts);

            if (start != -1) {
                start = start + lengthFront;
                sTextsStartList.add(start);
                lengthFront = start + sTextLength;
                temp = text.substring(lengthFront);
            }

        } while (start != -1);

        SpannableStringBuilder styledText = new SpannableStringBuilder(text);
        for (Integer i : sTextsStartList) {
            styledText.setSpan(
                    new ForegroundColorSpan(context.getResources().getColor(R.color.color_F54D53)),
                    i,
                    i + sTextLength,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        textView.setText(styledText);
    }

}
